package com.example.bankmanage.config;

import com.example.bankmanage.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;


@Configuration
@Slf4j
@SuppressWarnings("all")
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
//    @Autowired
//    private UserContextInterceptor userContextInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/account/list")
                .excludePathPatterns("/account/add")
                .excludePathPatterns("/account/update")
                .excludePathPatterns("/account/detail")
                .excludePathPatterns("/bill/getBillList")
                .excludePathPatterns("/bill/addBill")
                .excludePathPatterns("/bill/updateBill")
                .excludePathPatterns("/bill/deleteBill")
                .excludePathPatterns("/api/cards")
                .excludePathPatterns("/api/addCard")
                .excludePathPatterns("/api/updateCard")
                .excludePathPatterns("/api/login-logs")
                .excludePathPatterns("/api/system-logs")
                .excludePathPatterns("/transfer/records")
                .excludePathPatterns("/bill/exportBill")
                .excludePathPatterns("/api/exportCard")
                .excludePathPatterns("/user/empInfo");
    }
    //可以确保Spring Boot支持 application/json 和 application/x-www-form-urlencoded 等多种Content-Type
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
//        jsonConverter.setSupportedMediaTypes(Arrays.asList(
//                MediaType.APPLICATION_JSON,
//                MediaType.TEXT_PLAIN // 支持更多类型
//        ));
//        converters.add(jsonConverter);
//
//        FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
//        formConverter.setSupportedMediaTypes(Arrays.asList(
//                MediaType.APPLICATION_FORM_URLENCODED,
//                MediaType.MULTIPART_FORM_DATA
//        ));
//        converters.add(formConverter);
//    }

//    /**
//     * 跨域
//     * @param registry
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("http://localhost:5173")
//                .allowedMethods("*")
//                .allowedHeaders("*")
//                .allowCredentials(true);//是否允许了凭证 如：cookies
//    }*/

}

